/*

	This file prepares regressions sample at firm-destination-year level

*/

cd ${work}5_reg_analysis

use input\sample_idt if year>2002 , clear

*EXPORT
preserve
	use input\export_ipdt, clear
	collapse (sum) vrd_exp_exsmall , by(jurnr land year)
	save temp\temp, replace
restore
merge 1:1 jurnr land year using temp\temp, keep(1 3) keepusing(vrd_exp_exsmall) nogen
erase temp\temp.dta
replace vrd_exp_exsmall=0 if vrd_exp_exsmall==.

*PROMOTION
destring jurnr, gen(jurnr_num)
merge 1:1 jurnr_num land year using input\promotion_idt, keep(1 3) keepusing(treated_1 treated_2 treated_lag treated_lead tdcinitiative_1 tdcinitiative_2 tdcinitiative_lag tdcinitiative_lead itemvalue_1 itemvalue_lag discount_1 discount_lag) nogen
drop jurnr_num
foreach v of varlist *_1 *_2 *_lag {
    replace `v'=0 if `v'==.
}
foreach v of varlist *_lead {
    replace `v'=0 if `v'==. & year<2015
}

*INDUSTRY CODES
merge m:1 jurnr year using input\firm_it, assert(2 3) keep(3) keepusing(nace4) nogen

*LAGGED TOTAL SALES
preserve
	use input\firm_it, clear
	keep jurnr year sales
	replace sales=sales/1e6
	deflate sales
	replace year=year+1
	save temp\temp, replace
restore
merge m:1 jurnr year using temp\temp, assert(2 3) keep(3) keepusing(sales) nogen
erase temp\temp.dta	

*DESTINATION INDICATORS
merge m:1 land using input\income_d, assert(2 3) keep(3) keepusing(income_group) nogen
gen dum_income = inlist(income_group,"High income","Upper middle income")
drop income_group

*SPECIFICATOINS
winsor2 vrd_exp_exsmall if vrd_exp_exsmall>0 , cuts(0 99) replace
gen log_vrd_exp_exsmall=log(vrd_exp_exsmall)

gen exp_status = (vrd_exp_exsmall>0)

lab var log_vrd_exp_exsmall	"ln(EXV)"
lab var exp_status			"EXP"

*FEs
egen IDY=group(nace4 land year)
egen FY=group(jurnr year)
egen FD=group(jurnr land)
egen F=group(jurnr)

*SAVE
compress
save temp\regsample_idt, replace